//
//  subYHQController.m
//  LazyCat
//
//  Created by rocky on 15/12/14.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "subYHQController.h"
#import "yhqViewCell.h"
#import "editYHQViewController.h"
#import "AFNetworkHandler.h"
#import "FeWifiManHub.h"
#import "MJChiBaoZiFooter.h"
#import "MJChiBaoZiHeader.h"

@interface subYHQController ()<UITableViewDataSource,UITableViewDelegate>
{
    FeWifiManHub *_loading;
}
@property(nonatomic,strong) UIView *noYHQView;
@property(nonatomic,strong) NSMutableArray *dataArr;
@property(nonatomic,strong) NSString *page;//当前页
@property(nonatomic,strong) NSString *allpage;//总页数
@property (nonatomic,strong) UIView *noNetView;//无网络视图

@end

@implementation subYHQController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        [self.dataArr removeAllObjects];
        [self.tableView reloadData];
        self.noYHQView.hidden = YES;
        self.noNetView.hidden = NO;
    }else {
        self.noNetView.hidden = YES;
//        if ([AppData share].isLoadd) {
            [self getMess1];
//            [AppData share].isLoadd = NO;
//        }else {
//            
//        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = @"1";
    [AppData share].isLoadd = NO;
    //添加表视图
    self.view.backgroundColor = BackColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:[yhqViewCell class] forCellReuseIdentifier:@"Cell"];//注册
    //添加没有优惠券的视图
    self.noYHQView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-104)];
    self.noYHQView.hidden = YES;
    self.noYHQView.backgroundColor = BackColor;
    [self.view addSubview:self.noYHQView];
    UIImageView *noImageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"zanwuyouhuiquan"]];
    noImageV.frame = CGRectMake((ScreenWidth-100)/2, 20, 100, 75);
    [self.noYHQView addSubview:noImageV];
    UILabel *noYHQLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 105, ScreenWidth, 30)];
    noYHQLabel.text = @"抱歉,暂时还没有优惠券";
    noYHQLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];
    noYHQLabel.font = [UIFont systemFontOfSize:16];
    noYHQLabel.textAlignment = NSTextAlignmentCenter;
    [self.noYHQView addSubview:noYHQLabel];
    _loading = [[FeWifiManHub alloc] initWithView:self.view withMode:FeWifiManHubModeOnlyLoader];
    _loading.frame = CGRectMake(_loading.frame.origin.x, _loading.frame.origin.y - 104, _loading.frame.size.width, _loading.frame.size.height);
    [self.view addSubview:_loading];
    self.dataArr = [NSMutableArray array];//当前页数组
    [_loading show];//loading框
    self.noNetView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 104)];
    self.noNetView.hidden = YES;
    self.noNetView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.noNetView];
    UIImageView *netImg = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-100)/2, 20, 100, 90)];
    netImg.image = [UIImage imageNamed:@"无网络_r1_c1"];
    [self.noNetView addSubview:netImg];
    UILabel *netFail = [[UILabel alloc] initWithFrame:CGRectMake(0, 130, ScreenWidth, 20)];
    netFail.text = @"网络请求失败";
    netFail.textAlignment = NSTextAlignmentCenter;
    netFail.font = [UIFont systemFontOfSize:16];
    [self.noNetView addSubview:netFail];
    UIButton *refresh = [[UIButton alloc] initWithFrame:CGRectMake((ScreenWidth-120)/2, 160, 120, 40)];
    [refresh setImage:[UIImage imageNamed:@"无网络_r3_c2 下午4.23.04"] forState:UIControlStateNormal];
    refresh.adjustsImageWhenHighlighted = NO;
    [self.noNetView addSubview:refresh];
    [refresh addTarget:self action:@selector(refreshV) forControlEvents:UIControlEventTouchUpInside];//刷新
//    [self getMess1];
//    [self downRefresh];//下拉加载
    self.tableView.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    [self upRefresh];//上拉刷新
}
-(void)refreshV {
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        [self.dataArr removeAllObjects];
        [self.tableView reloadData];
        self.noYHQView.hidden = YES;
        self.noNetView.hidden = NO;
    }else {
        self.noNetView.hidden = YES;
        [self getMess1];//获取数据
    }
}
-(void)downRefresh {//下拉刷新
//    MJChiBaoZiHeader *header = [MJChiBaoZiHeader headerWithRefreshingBlock:^{
//        [header beginRefreshing];
        [self getMess2];
//    }];
//    self.tableView.header = header;
//    header.lastUpdatedTimeLabel.hidden = YES;
}
-(void)upRefresh {//上拉加载
    MJRefreshAutoFooter *foot = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        if ([self.page integerValue] >= [self.allpage integerValue]) {
            [[MessShowView share]showWithMess:@"没有更多数据"];
            return ;
        }
        NSInteger nextPage = [self.page integerValue] + 1;
        self.page = [NSString stringWithFormat:@"%ld",(long)nextPage];
        [foot beginRefreshing];
        [self getMess];//仅用于上拉加载
    }];
    self.tableView.footer = foot;
}
-(void)panduan {//判断
    if (self.dataArr.count < 1) {
        self.noYHQView.hidden = NO;
    }else {
        self.noYHQView.hidden = YES;
    }
}
-(void)getMess {//上拉加载
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        [self.dataArr removeAllObjects];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [_loading dismiss];
        [_loading removeFromSuperview];
        self.noYHQView.hidden = YES;
        self.noNetView.hidden = NO;
        return;
    }
    NSString *staues;//初始化状态
    if (_currentPage == 0) {
        staues = @"1";
    }else if (_currentPage == 1) {
        staues = @"3";
    }else if (_currentPage == 2){
        staues = @"4";
    }
    NSLog(@"%@    %@",staues,self.page);
    NSString *url = [NSString stringWithFormat:YhqUrl,@"20",self.page,[AppData share].username,staues];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = result;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            NSArray *arr = [NSArray array];//当前页数组
            arr = dic[@"coupons"];
            [self.dataArr addObjectsFromArray:arr];
            self.allpage = dic[@"totalpage"];
            NSLog(@"%@,%@",self.page,self.allpage);
            
            [self panduan];//判断
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
            [_loading dismiss];
            [_loading removeFromSuperview];
        }
    }];
}
-(void)getMess2 {//下拉刷新
    self.page = @"1";
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        [self.dataArr removeAllObjects];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [_loading dismiss];
        [_loading removeFromSuperview];
        
        self.noYHQView.hidden = YES;
        self.noNetView.hidden = NO;
        return;
    }
    NSString *staues;//初始化状态
    if (_currentPage == 0) {
        staues = @"1";
    }else if (_currentPage == 1) {
        staues = @"3";
    }else if (_currentPage == 2){
        staues = @"4";
    }
    NSLog(@"%@    %@",staues,self.page);
    NSString *url = [NSString stringWithFormat:YhqUrl,@"20",self.page,[AppData share].username,staues];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = result;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            [self.dataArr removeAllObjects];
            self.dataArr = dic[@"coupons"];
            self.allpage = dic[@"totalpage"];
            NSLog(@"%@=================%@",self.page,self.allpage);
            [self panduan];//判断
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
            [_loading dismiss];
            [_loading removeFromSuperview];
        }
    }];
    [self.tableView.header endRefreshing];
}
-(void)getMess1 {//获取数据
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        [self.dataArr removeAllObjects];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [_loading dismiss];
        [_loading removeFromSuperview];
        
        self.noYHQView.hidden = YES;
        self.noNetView.hidden = NO;
        return;
    }
    NSString *staues;//初始化状态
    if (_currentPage == 0) {
        staues = @"1";
    }else if (_currentPage == 1) {
        staues = @"3";
    }else if (_currentPage == 2){
        staues = @"4";
    }
    NSLog(@"%@    %@",staues,self.page);
    NSString *url = [NSString stringWithFormat:YhqUrl,@"20",self.page,[AppData share].username,staues];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = result;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            self.dataArr = dic[@"coupons"];
            self.allpage = dic[@"totalpage"];
            NSLog(@"%@,%@",self.page,self.allpage);
            
            [self panduan];//判断
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
            [_loading dismiss];
            [_loading removeFromSuperview];
        }
    }];
}
#pragma mark - Table view data source//这里有好多个分区每个分区都只有一个
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    yhqViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_currentPage == 2) {
        cell.titleL.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];
        cell.firstImageV.image = [UIImage imageNamed:@"huisebeijing"];
    }else {
        cell.titleL.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
        cell.firstImageV.image = [UIImage imageNamed:@"huangsebeijing"];
    }
    if (self.dataArr.count > 0) {//判断
        cell.titleL.text = [NSString stringWithFormat:@"%@",[self.dataArr[indexPath.section] objectForKey:@"title"]];
        cell.timeL.text = [NSString stringWithFormat:@"有效期:%@~%@",[self.dataArr[indexPath.section] objectForKey:@"startdate"],[self.dataArr[indexPath.section] objectForKey:@"expiredate"]];
        float mianzhi = [[self.dataArr[indexPath.section] objectForKey:@"strc"] floatValue];
        cell.moneyL.text = [NSString stringWithFormat:@"%.0f",mianzhi];
        cell.hasUseL.text = [NSString stringWithFormat:@"已使用:%@人",[self.dataArr[indexPath.section] objectForKey:@"hadused"]];
        cell.lqCountL.text = [NSString stringWithFormat:@"领取人数:%@人",[self.dataArr[indexPath.section] objectForKey:@"hadreceived"]];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {//点击单元格跳转
    if (self.dataArr.count > 0) {//判断数组数量避免越界
        NSLog(@"%ld,%ld",(long)self.currentPage,(long)indexPath.section);
        if (_currentPage == 2) {
            editYHQViewController *editVC = [[editYHQViewController alloc]init];
            editVC.identify = [NSString stringWithFormat:@"%@",[self.dataArr[indexPath.section] objectForKey:@"couponxpostrecordid"]];
            editVC.isFrom = @"no";
            editVC.dict = self.dataArr[indexPath.section];//传递字典
            if (_myBlock) {
                _myBlock(editVC);
            }
        }else {
            editYHQViewController *editVC = [[editYHQViewController alloc]init];
            editVC.identify = [NSString stringWithFormat:@"%@",[self.dataArr[indexPath.section] objectForKey:@"couponxpostrecordid"]];
            editVC.isFrom = @"yes";
            editVC.dict = self.dataArr[indexPath.section];//传递字典
            if (_myBlock) {
                _myBlock(editVC);
            }
        }
    }else {
        
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {//单元格高度
    return 90;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
